package com.stylefeng.guns.modular.system.task;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.modular.system.model.Gdtj;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.model.Sm;
import com.stylefeng.guns.modular.system.service.IGdtjService;
import com.stylefeng.guns.modular.system.service.IGpdataService;
import com.stylefeng.guns.modular.system.service.ISmService;

@Service
public class SmImportTask {
	
	@Autowired
    private IGpdataService gpdataService;
	
	@Autowired
    private IGdtjService gdtjService;
	
	@Autowired
    private ISmService smService;
	
	
	public void execute(boolean isHoliday) {
		List<Gpdata> gpdatas =  gpdataService.selectList(null);
		for(Gpdata gpdate : gpdatas){
			Wrapper<Gdtj> gdtjW = new EntityWrapper<Gdtj>();
			gdtjW.eq("g_dm", gpdate.getgDm());
			gdtjW.eq("g_sharestype", "A股");
			List<Gdtj> gdtjs = gdtjService.selectList(gdtjW);
			for(Gdtj gdtj : gdtjs){
				if(jugdeRm(gdtj.getgSharehdname())){
					int count = gdtjService.selectCountByName(gdtj.getgSharehdname());
					if(count > 1){
						Wrapper<Sm> smW = new EntityWrapper<Sm>();
						smW.eq("g_name", gdtj.getgSharehdname());
						Sm sm = smService.selectOne(smW);
						if(sm == null){
							sm = new Sm();
							sm.setgName(gdtj.getgSharehdname());
							sm.setgHbl(new Double(50));
							sm.setgZjl(new Double(1));
							smService.insert(sm);
						}
					}
				}
			}
		}
		
	}
	
	private boolean jugdeRm(String name){
		int count = 0;
		char[] c = name.toCharArray();
        for(int i = 0; i < c.length; i ++)
        {
            String len = Integer.toBinaryString(c[i]);
            if(len.length() > 8)
                count ++;
        }
        return count <= 3;
	}
}
